home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / TarIO.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  928 b   |  48 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: TarIO.py,v 1.1.1.2 1999/01/13 09:40:21 sjoerd Exp $
  4. #
  5. # read files from within a tar file
  6. #
  7. # History:
  8. # 95-06-18 fl    Created
  9. # 96-05-28 fl    Open files in binary mode
  10. #
  11. # Copyright (c) Secret Labs AB 1997.
  12. # Copyright (c) Fredrik Lundh 1995-96.
  13. #
  14. # See the README file for information on usage and redistribution.
  15. #
  16.  
  17. import ContainerIO
  18. import string
  19.  
  20. class TarIO(ContainerIO.ContainerIO):
  21.  
  22.     def __init__(self, tarfile, file):
  23.  
  24.     fh = open(tarfile, "rb")
  25.  
  26.     while 1:
  27.  
  28.         s = fh.read(512)
  29.         if len(s) != 512:
  30.         raise IOError, "unexpected end of tar file"
  31.  
  32.         name = s[:100]
  33.         i = string.find(name, chr(0))
  34.         if i == 0:
  35.         raise IOError, "cannot find subfile"
  36.         if i > 0:
  37.         name = name[:i]
  38.  
  39.         size = string.atoi(s[124:136], 8)
  40.  
  41.         if file == name:
  42.         break
  43.  
  44.         fh.seek((size + 511) & (~511), 1)
  45.  
  46.     # Open region
  47.     ContainerIO.ContainerIO.__init__(self, fh, fh.tell(), size)
  48.